# 使用多阶段构建减小镜像大小
FROM maven:3.8.6-openjdk-11 AS build
WORKDIR /workspace

# 创建 .m2 目录并添加阿里云镜像配置
RUN mkdir -p /root/.m2 && \
    echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"><mirrors><mirror><id>aliyun</id><name>aliyun maven</name><url>https://maven.aliyun.com/repository/public</url><mirrorOf>central</mirrorOf></mirror></mirrors></settings>' > /root/.m2/settings.xml

# 复制项目文件（整个项目）
COPY . .

# 构建项目（跳过测试）- 只构建 user-service 模块
RUN mvn clean package -pl user-service -am -DskipTests

# 最终运行镜像
FROM openjdk:11-jre-slim
WORKDIR /app

# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 从构建阶段复制jar文件
COPY --from=build /workspace/user-service/target/user-service-*.jar app.jar

# 设置环境变量
ENV TZ=Asia/Shanghai \
    SPRING_PROFILES_ACTIVE=default \
    SPRING_CLOUD_NACOS_DISCOVERY_SERVER_ADDR=nacos-server.todo-system.svc.cluster.local:8848 \
    SPRING_CLOUD_NACOS_CONFIG_SERVER_ADDR=nacos-server.todo-system.svc.cluster.local:8848 \
    SERVER_PORT=8080

# 暴露端口
EXPOSE 8080

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s \
  CMD curl -f http://localhost:8080/actuator/health || exit 1

# 启动应用
ENTRYPOINT ["java", "-jar", "app.jar"]